<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_xml_headers() {
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate('D, d M Y H:i:s') . " GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	header("Content-type: text/xml; charset=UTF8");
}

function func_xml_close() {
	exit();
}

function func_xml_output($content, $direct = true, $root_tag = 'result') {
	$output_str = '';
	
	$output_str .= '<?xml version="1.0" encoding="utf-8"?>' . "\n";
	$output_str .= '<' . $root_tag . '>' . $content . '</' . $root_tag . '>';
	
	if( $direct == true ) {
		echo $output_str;
	} else {
		return $output_str;
	}
}

function func_xml_album_output($albums_id) {
	global $galleryObj;
	
	$album = $galleryObj->getAlbum($albums_id, 'albums_id');
	$images = $galleryObj->getImages($albums_id, 'albums_id');

	$output_str = '';
	$output_str .= '<galleries>' . "\n";
	$output_str .= '<gallery>' . "\n";
	
	$output_str .= '<id>' . $album['albums_id'] . '</id>' . "\n";
	$output_str .= '<link>' . $album['albums_id'] . '</link>' . "\n";
	$output_str .= '<name><![CDATA[' . $album['albums_name'] . ']]></name>' . "\n";
	$output_str .= '<description><![CDATA[' . $album['albums_description'] . ']]></description>' . "\n";
	$output_str .= '<author><![CDATA[' . $album['albums_owner'] . ']]></author>' . "\n";
	
	if( sizeof($images) > 0 ) {
		$output_str .= '<photos>' . "\n";

		for($i=0, $n=sizeof($images); $i<$n; $i++) {

			$output_str .= '<photo>' . "\n";
			$output_str .= '<id>' . $images[$i]['images_id'] . '</id>' . "\n";
			$output_str .= '<name><![CDATA[' . $images[$i]['images_name'] . ']]></name>' . "\n";
			$output_str .= '<description><![CDATA[' . $images[$i]['images_description'] . ']]></description>' . "\n";
			$output_str .= '<source><![CDATA[' . CONFIG_URL . CONFIG_DIR_ALBUMS . $images[$i]['albums_id'] . '/' . $images[$i]['images_value'] . ']]></source>' . "\n";
			$output_str .= '<width><![CDATA[' . $images[$i]['images_width'] . ']]></width>' . "\n";
			$output_str .= '<height><![CDATA[' . $images[$i]['images_height'] . ']]></height>' . "\n";
			$output_str .= '<viewed><![CDATA[' . $images[$i]['images_viewed'] . ']]></viewed>' . "\n";
			$output_str .= '</photo>' . "\n";
		}
		$output_str .= '</photos>' . "\n";
	}
	$output_str .= '</gallery>' . "\n";
	$output_str .= '</galleries>' . "\n";
	
	return $output_str;
}

function func_xml_database_tables_output() {
	global $adb;
	
	$tables = $adb->getTables();
	
	d($tables);


}

function func_xml_database_table_fields_output($table) {
	global $adb;
	
	$metadata = $adb->getMetadata($table);

	$output_str = '';
	$output_str .= '<fields>';
	if( is_array($metadata) ) {
		$fields_num = $metadata['fields_num'];
		for($i=0, $n=sizeof($metadata); ($i<$n && $i<$fields_num); $i++) {
			$output_str .= '<field>';
			$output_str .= '<name>' . $metadata[$i]['name'] . '</name>' . "\n";
			$output_str .= '<type>' . $metadata[$i]['type'] . '</type>' . "\n";
			$output_str .= '<len>' . $metadata[$i]['len'] . '</len>' . "\n";
			$output_str .= '<flags>' . $metadata[$i]['flags'] . '</flags>' . "\n";
			$output_str .= '</field>';
		}
	}
	$output_str .= '</fields>';
	
	return $output_str;
}
//
?>